﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using CommonLib.BLL;

public partial class CRM_Store_ERP_PerReStore : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState_OrderSre = "InDate desc";
            AddHandlersToControls();
            StoreBind();
            //BindCenterStore(Store_Rome, Request.QueryString["tabid"]);
            gridPanel_listdata.Reload();
        }
    }

    private void StoreBind()
    {
        if (BS_PerStore == 1)
        {
            DataTable dtStore = CommonLib.BLL.BS_DATASET_SQL.SqlGetTable("select * from VIEW_STORE where STOREROOM_LEVEL='中心库房' ");
            Store_Rome.DataSource = dtStore;
            Store_Rome.DataBind();
            DataTable dtUser = CommonLib.BLL.SYS_USER.GetList("USER_ID='" + BS_LoginUserID + "'").Tables[0];
            Store_USER.DataSource = dtUser;
            Store_USER.DataBind();
        }
        else
        {
            if (BS_LoginRoles.Contains("01") || BS_LoginRoles.Contains("02"))
            {
                BindCenterStore(Store_Rome, Request.QueryString["tabid"]);
                DataTable dtUser = CommonLib.BLL.SYS_USER.GetList("IS_MANAGER=1").Tables[0];
                Store_USER.DataSource = dtUser;
                Store_USER.DataBind();
            }
        }
    }

    private void AddHandlersToControls()
    {
        string Url = G_Url_allstring + "/CRM_Store/ERP_PerReStoreEdit.aspx";

        string tabid = base.G_FrameTabid;
        //popfortab(GridName, Url, StringName, StringField, RowId, AttQryStr, TabId, TabName, flag)
        string handler = "popfortab(gridPanel_listdata, '{0}', '{1}', '{2}', {3}, '{4}', '{5}', '{6}', {7});";
        extBtn_Add.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "Add", 0, "", tabid, "退货单（详细）", 1);
        extBtn_Edit.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "PerReturnId", 0, "", tabid, "退货单（详细）", 2);
        gridPanel_listdata.Listeners.RowDblClick.Handler = string.Format(handler, Url, "ObjId", "PerReturnId", "rowIndex", "", tabid, "退货单（详细）", 3);
    }

    string _QueryString;
    private string GetQryString()
    {
        string result = " 1=1 ";

        if (exttxtf_PerReturnNo.Text.Trim() != "")
            result += " and PerReturnNo like'%" + FilterSpecChar(exttxtf_PerReturnNo.Text.Trim()) + "%'";
        if (extcomb_STOREROOM_ID.SelectedItem.Text.Trim() != "")
            result += " and StoreRoomId ='" + FilterSpecChar(extcomb_STOREROOM_ID.SelectedItem.Value.Trim()) + "'";
        if (extcomb_USER.SelectedItem.Text.Trim() != "")
            result += " and Owner ='" + FilterSpecChar(extcomb_USER.SelectedItem.Value.Trim()) + "'";
        if (extcomb_State.SelectedItem.Text != "" && extcomb_State.SelectedItem.Value != "-1")
            result += " and State ='" + FilterSpecChar(extcomb_State.SelectedItem.Value.Trim()) + "'";
        if (extdate_start.SelectedValue != null)
            result += " and InDate >='" + extdate_start.Value.ToString() + "'";
        if (extdate_end.SelectedValue != null)
            result += " and InDate <='" + extdate_end.Value.ToString() + "'";
        return result;
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        _QueryString = GetQryString();
        int count = CRMMis.BLL.ERP_PerReStore.GetRecorderCount(_QueryString);
        BindGridPSData(e, ViewState_OrderSre, PagingToolbar1, Store1, count);
    }

    public override DataTable GetPSData(int PageSize, int CurPage, string OrderStr)
    {
        ViewState_OrderSre = OrderStr;
        DataTable dt = CRMMis.BLL.ERP_PerReStore.GetPageList(PageSize, CurPage, _QueryString, OrderStr).Tables[0];
        return dt;
    }

    [DirectMethod]
    public void DeleteModel(int PerReturnId)
    {
        CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
        try
        {
            var model = CRMMis.BLL.ERP_PerReStore.GetModel(PerReturnId);
            //判断是否有审核员角色
            if (BS_LoginRoles.Contains("02"))
            {
                if (model.State == 2)
                {
                    X.Msg.Alert("系统提示", "已审核的数据不能删除！").Show();
                    return;
                }
            }
            else    //普通用户
            {
                if (model.State > 0)
                {
                    X.Msg.Alert("系统提示", "已提交的数据不能删除！").Show();
                    return;
                }
            }
            myTrans.BeginTransaction();
            CRMMis.BLL.ERP_PerReStoreDetail.DeleteFK(myTrans, "PerReturnId = " + PerReturnId);
            CRMMis.BLL.ERP_PerReStore.Delete(myTrans, PerReturnId);
            myTrans.CommitTran();
            X.Msg.Alert("系统提示", "删除成功！").Show();
            gridPanel_listdata.Reload();

        }
        catch (Exception ex)
        {
            myTrans.RollBackTran();
            X.Msg.Alert("系统提示", "删除失败！</br>" + ex.Message).Show();
        }
    }
}